package com.jk.myimkit.eventbus.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import com.jk.myimkit.eventbus.EventBus;

public class ErrorDialogFragments {
    public static int ERROR_DIALOG_ICON = 0;
    public static Class<?> EVENT_TYPE_ON_CLICK;

    public ErrorDialogFragments() {
    }

    public static Dialog createDialog(Context context, Bundle arguments, DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(arguments.getString("de.greenrobot.eventbus.errordialog.title"));
        builder.setMessage(arguments.getString("de.greenrobot.eventbus.errordialog.message"));
        if (ERROR_DIALOG_ICON != 0) {
            builder.setIcon(ERROR_DIALOG_ICON);
        }

        builder.setPositiveButton(17039370, onClickListener);
        return builder.create();
    }

    public static void handleOnClick(DialogInterface dialog, int which, Activity activity, Bundle arguments) {
        if (EVENT_TYPE_ON_CLICK != null) {
            Object event;
            try {
                event = EVENT_TYPE_ON_CLICK.newInstance();
            } catch (Exception var6) {
                throw new RuntimeException("Event cannot be constructed", var6);
            }

            EventBus eventBus = ErrorDialogManager.factory.config.getEventBus();
            eventBus.post(event);
        }

        boolean finish = arguments.getBoolean("de.greenrobot.eventbus.errordialog.finish_after_dialog", false);
        if (finish && activity != null) {
            activity.finish();
        }

    }

    public static class Support extends DialogFragment implements DialogInterface.OnClickListener {
        public Support() {
        }

        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return ErrorDialogFragments.createDialog(this.getActivity(), this.getArguments(), this);
        }

        public void onClick(DialogInterface dialog, int which) {
            ErrorDialogFragments.handleOnClick(dialog, which, this.getActivity(), this.getArguments());
        }
    }

    @TargetApi(11)
    public static class Honeycomb extends android.app.DialogFragment implements DialogInterface.OnClickListener {
        public Honeycomb() {
        }

        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return ErrorDialogFragments.createDialog(this.getActivity(), this.getArguments(), this);
        }

        public void onClick(DialogInterface dialog, int which) {
            ErrorDialogFragments.handleOnClick(dialog, which, this.getActivity(), this.getArguments());
        }
    }
}
